//
// File.h
// Usage: VFS file object
// CreateDate: 2009-05-06
// Author: MaJiuyue
//
#ifndef _FILE_H_
#define _FILE_H_

#include <type.h>
#include <string.h>
#include "FileSystem.h"

enum FILE_TYPE { FT_REGULAR, FT_DIRECTORY, FT_DEVICE };
class File
{
protected:
	char name[128];	  // File name
	u32 inode;	  // Device-specific - for filesystem to identify files.
	size_t length;	  // Size of the file, in bytes.
	int ref_count;    // File ref count
	FILE_TYPE type;

	// FileSystem
	FileSystem * fs;

public:
	File(const char *name, u32 inode, u32 length, FileSystem * fs, FILE_TYPE type)
	{
		strcpy(this->name, name);
		this->inode = inode;
		this->length = length;
		this->fs = fs;
		this->ref_count = 0;
		this->type = type;
	}
	virtual ~File(){}

public:
	const char * GetFileName() { return name; }
	u32 GetInode() { return inode; }
	size_t GetLength() { return length; }
	FILE_TYPE GetType() { return type; }

/* All interface define */
public:
	virtual void open() { ref_count++; }
	virtual void close() { if(ref_count>0) ref_count--; }
	virtual int read(int off, size_t size, addr_t buf)=0;
	virtual int write(int off, size_t size, addr_t buf)=0;
};

#endif

